//
//  HMMainViewController.swift
//  WeiBo
//
//  Created by Apple on 16/9/18.
//  Copyright © 2016年 KY. All rights reserved.
//

import UIKit

class HMMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        //  添加子视图控制器的2种方式
        //  1.
//        addChildViewController(xx)
        //  2.
//        viewControllers
        
        //  设置tabbar的tintColor, 设置tabbar按钮的选中颜色
//        UITabBar.appearance().tintColor = UIColor.orange
        
        
        let hmTabBar = HMTabBar()
        //  设置代理对象
        hmTabBar.hmDelegate = self
        //  使用self会产生循环引用，使用[weak self]解决循环引用
        hmTabBar.composeButtonClosure = { [weak self] in
            print(self)
            print("我是闭包调用过来的")
        }
        
        //  只读属性不能设置值
//        self.tabBar = hmTabBar
        //  kvc设置给oc类的只读属性赋值
        setValue(hmTabBar, forKey: "tabBar")
        
        
        addChildViewController(HMHomeTableViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(HMMessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(HMDiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(HMProfileTableViewController(), title: "我的", imageName: "tabbar_profile")
        
    }
    //  添加子视图控制器
    func addChildViewController(_ childController: UIViewController, title: String, imageName: String) {
        
//        if title == "发现" {
//            childController.tabBarItem.imageInsets = UIEdgeInsets(top: 5, left: 0, bottom: -5, right: 0)
//            
//        } else {
//            //  设置标题
//            childController.tabBarItem.title = title
//            childController.navigationItem.title = title
//        }
        
        //  设置标题
//        childController.tabBarItem.title = title
//        childController.navigationItem.title = title
        //  合并一句代码
        childController.title = title
        
        
        //  设置tabbarItem的图片
        childController.tabBarItem.image = UIImage(named: imageName)
        //  修改选中图片的渲染模式
        childController.tabBarItem.selectedImage = UIImage(named: "\(imageName)_selected")?.withRenderingMode(.alwaysOriginal)
        //  设置选中文字颜色的
        childController.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orange], for: .selected)
        //  设置字体大小
        childController.tabBarItem.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFont(ofSize: 13)], for: .normal)
        
        
        //  创建导航控制器
        let nav = HMNavigationViewController(rootViewController: childController)
        //  添加子视图控制器
        addChildViewController(nav)
        
        
    }
    

    

}

// MARK:    --HMTabBar代理方法
extension HMMainViewController: HMTabBarDelegate {
    func didClickComposeButton() {
        print("我是代理对象调用过来的")
    }

}



















